import requests
from bs4 import BeautifulSoup
import chardet

def get_site_info(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        content = response.content

        # 利用 chardet 库检测编码
        detected_encoding = chardet.detect(content)['encoding']
        if detected_encoding:
            response.encoding = detected_encoding
        else:
            response.encoding = response.apparent_encoding

        soup = BeautifulSoup(response.text, 'html.parser')
        site_name = soup.title.string if soup.title else "No title found"

        links = []
        for link in soup.find_all('a'):
            href = link.get('href')
            if href:
                links.append(href)

        return site_name, links
    except requests.RequestException as e:
        print(f"请求出错: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")
    return None, []


if __name__ == "__main__":
    target_url = "https://www.baidu.com"
    name, site_links = get_site_info(target_url)
    print(f"网站名: {name}")
    print("链接:")
    for link in site_links:
        print(link)